<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Check;
use App\Admin\Models\Users;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use function foo\func;
use App\Admin\Actions\Authentication\Agree;
use App\Admin\Actions\Authentication\Refuse;

class UserdController extends AdminController
{
    protected $title = '代理商列表';

    protected function grid()
    {
        $grid = new Grid(new Users);
        $grid->disableCreateButton();
//        $grid->disableActions();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableedit();
        });
        $grid->model()->where('level',1);
        $grid->filter(function($filter){
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->equal('account', 'UID');
            });
            $filter->column(1/3, function ($filter) {
                $filter->like('nickname','微信昵称');
            });

        });
        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $page = request('page');
            if($page){
                $page = ($page-1)*10;
            }
            $row->column('number',$page + $number+1);
        });
        $grid->column('account','用户UID');
        $grid->column('watchid','微信Openid');
        $grid->column('avatar', '头像')->lightbox(['width' => 40, 'height' => 40]);
//        $grid->challenge_map()->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('nickname','微信昵称');
//        $grid->column('realname','真实姓名');
//        $grid->column('realname','真实姓名');
////        $grid->column('mobile','电话');
//        $grid->column('card','身份证号');
//        $grid->column('card_img', '身份证正面照')->lightbox(['width' => 40, 'height' => 40]);
//        $grid->column('card_imgs', '身份证背面照')->lightbox(['width' => 40, 'height' => 40]);
//        $grid->column('image', '免冠照')->lightbox(['width' => 40, 'height' => 40]);
        $grid->column('level','用户身份')->display(function ($level){
            switch ($level){
                case 0:
                    return "普通用户";
                    break;
                case 1:
                    return "代理商";
                    break;
            }
        });
        $grid->column('admin_name','后端账号');
        $grid->column('is_auth','是否认证')->using([
            1 => '未认证',
            2 => '认证中',
            3 => '拒绝',
            4 => '已认证',
        ])->dot([
            1 => 'primary',
            2 => 'info',
            3 => 'danger',
            4 => 'success'
        ]);

        return $grid;
    }

    protected function detail($id){
        $show = new Show(Users::findOrFail($id));
        $show->field('realname','真实姓名');
        $show->field('card','身份证号');
        $show->card_img('身份证正面照')->image('card_img', '身份证正面照');
        $show->card_imgs('身份证正面照')->image('card_imgs', '身份证反面照');
        $show->image('免冠照')->image('image', '免冠照');
        return $show;
    }
//    public function check(Request $request){
//        $id = $request->post('id');
//        $status = $request->post('stat');
//        $note = $request->post('reason');
//        if($status != Users::USER_STATUS_REFUSE && $status != Users::USER_STATUS_SUCCESS){
//            return ['code'=>'error','msg'=>'参数无效'];
//        }
//        $uinfo = Users::find($id);
//        if($uinfo->is_auth != 2){
//            return ['code' => 'error','msg' => '异常操作'];
//        }
//
//        $uinfo->is_auth = $status;
//        $uinfo->note = $note;
//        $uinfo->updated_at = date('Y-m-d H:i:s',time());
//        if($uinfo->save()){
//            return ['code' => 'success','msg' => '审核成功'];
//        }else{
//            return ['code' => 'error','msg' => '审核失败'];
//        }
//    }
}
